package com.sfx.concurrent.demo.concurrent.ParkUnPark;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.locks.LockSupport;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: sfx
 * Date: 2023-01-01
 * Time: 14:38
 */

@Slf4j(topic = "c.TestParkUnPark")
public class TestParkUnPark {
    public static void main(String[] args) throws InterruptedException {
        /**
         * 我们这个unpark既可以在park之前调用,也可以在park之后调用
         * 用来恢复暂停线程的运行
         */
        Thread t1 = new Thread(() -> {
            log.debug("开始");
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            log.debug("park");
            LockSupport.park();//暂停当前线程
            log.debug("resume...");
        },"t1");
        t1.start();
        Thread.sleep(2000);
        log.debug("unPark");
        LockSupport.unpark(t1);//恢复t1线程的运行
    }
}